<template>
  <div></div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import { outLogin, getUser } from './api/outLogin'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { urlToJson } from '@/utils/url'
import { servicesLoading } from '@/utils/loading'
import { ElMessage } from 'element-plus'
import { forEach } from 'lodash'

export default defineComponent({
  setup() {
    const router = useRouter()
    const store = useStore()
    let params = location.href.slice(location.href.indexOf('?') + 1)
    console.log(params, 'params')

    let data = urlToJson() as any
    console.log(data, data.approve, 'data')

    const outLoading = servicesLoading({ fullscreen: false, text: '正在请求外部登录' })
    outLogin({
      userId: decodeURIComponent(data.userId),
      // userId:"sF4byO5zvgsUuCO2syLbRKNVRbLP1m3iQFo6DF2+cxH9+H0wWe7VGU+7j6wGTNKMxcn/bW8BdI6CWfeHIw+KGt1PLmsuSNybPiWyJCgflXUHqtnaTlgVrYhi+Xk5eGQZ4ttRq38ruxCtrPsNUzNzjOOIEnQK5X70Ugxv5rnTeMJU0DpeaqCOHMP43KfPFkCJyBmXM65EUyvjdaZmPuGr1Y614hoDXrCF2A2BaAtu+WJAwvz5U4w7SN2WMmaRwD+evA95IRKX9cE+atqcayg9gakqou6JTQXoINZH978d7Z/DK5KniVUbuoXlEs/etecspk1osTRryxzupAgve5syqw=="
    })
      .then((res) => {
        outLoading.close()
        console.log(res, 'outLogin')
        localStorage.setItem('token', res.data.data)
        console.log(!data.approve, '!data.approve')

        if (!data.approve) {
          getUser().then((res) => {
            localStorage.setItem('permissions', JSON.stringify(res.data.data.authority))
            localStorage.setItem('userInfo', JSON.stringify(res.data.data.sysUser))
            store.state.system.userInfo = res.data.data.sysUser
          })
          const loading = servicesLoading({ fullscreen: false, text: '正在加载路由菜单' })
          store
            .dispatch('GenerateRoutes')
            .then((accessRoutes) => {
              console.log(accessRoutes, 'router.getRoutes()')
              if (!accessRoutes.length) {
                ElMessage({
                  message: '该用户没有任何菜单权限！',
                  type: 'warning',
                })
                localStorage.removeItem('token')
                localStorage.removeItem('userInfo')
                localStorage.removeItem('permissions')
                localStorage.removeItem('routes')
                localStorage.removeItem('zhongziConfig')

                store.state.router.routes = []
                router.push('/login')
                return
              }
              // 根据roles权限生成可访问的路由表
              forEach(accessRoutes, (route) => {
                // console.log(route, 'route')
                router.addRoute('home', route) // 动态添加可访问路由表
              })
              router.replace('/')
              loading.close()
            })
            .catch((error) => {
              loading.close()
              console.error(error)
            })
          // router.push('/')
        } else {
          delete data.userId
          delete data.approve
          // 跳转审批页面
          router.push({
            path: '/outExamine',
            query: {
              ...data,
            },
          })
        }
      })
      .catch(() => {
        outLoading.close()
      })

    return {}
  },
})
</script>

<style scoped></style>
